package com.cal.utms.model.payment;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import com.cal.utms.action.user_maintenance.LoginAction;
import com.cal.utms.dao.maintenance.CustomerDaoImpl;
import com.cal.utms.dao.maintenance.IssuerDaoImpl;
import com.cal.utms.dao.payment.JournalEntryDaoImpl;
import com.cal.utms.dto.payment.AccountDto;
import com.cal.utms.dto.payment.JounalEntryDto;
import com.cal.utms.entity.Accounting;
import com.cal.utms.entity.Journalentry;
import com.cal.utms.entity.MoneyTransaction;

public class JournalEntryModel {

	private JournalEntryDaoImpl daoImpl = new JournalEntryDaoImpl();
	private MoneyTransactionModel transactionModel = new MoneyTransactionModel();
	SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH);
	private CustomerDaoImpl customerDaoImpl = new CustomerDaoImpl();
	private IssuerDaoImpl issuerDaoImpl = new IssuerDaoImpl();

	public List<AccountDto> getAccountings() {
		List<AccountDto> accountDtos = new ArrayList<AccountDto>();
		List<Accounting> accountings = daoImpl.getAllAccountings();
		if (accountings.size() > 0) {
			for (Accounting accounting : accountings) {
				AccountDto accountDto = new AccountDto(accounting.getId(), accounting.getAccountNo(), accounting.getDescription());
				accountDtos.add(accountDto);
			}
		}
		return accountDtos;
	}

	public void saveJournalEntry(JounalEntryDto jounalEntryDto) {

		if (jounalEntryDto != null) {
			Journalentry journalentry = new Journalentry();
			journalentry.setAccountNo1(jounalEntryDto.getAccountNo1());
			journalentry.setAccountNo2(jounalEntryDto.getAccountNo2());
			journalentry.setAccountNo3(jounalEntryDto.getAccountNo3());
			if (jounalEntryDto.getAmount() != null) {
				String amount = jounalEntryDto.getAmount().replaceAll(",", "");
				double d = Double.parseDouble(amount);
				journalentry.setAmount(BigDecimal.valueOf(d));
			}
			if (jounalEntryDto.getAmount1() != null) {
				String amount = jounalEntryDto.getAmount1().replaceAll(",", "");
				double d = Double.parseDouble(amount);
				journalentry.setAmount1(BigDecimal.valueOf(d));
			}
			if (jounalEntryDto.getAmount2() != null) {
				String amount = jounalEntryDto.getAmount2().replaceAll(",", "");
				double d = Double.parseDouble(amount);
				journalentry.setAmount2(BigDecimal.valueOf(d));
			}
			if (jounalEntryDto.getAmount3() != null) {
				String amount = jounalEntryDto.getAmount3().replaceAll(",", "");
				double d = Double.parseDouble(amount);
				journalentry.setAmount3(BigDecimal.valueOf(d));
			}
			journalentry.setCreditDebit(jounalEntryDto.getCreditDebit());
			journalentry.setCustoemrName(jounalEntryDto.getCustoemrName());
			journalentry.setCustomerId(jounalEntryDto.getCustomerId());
			journalentry.setCustomerType(jounalEntryDto.getCustomerType());
			journalentry.setDescription1(jounalEntryDto.getDescription1());
			journalentry.setDescription2(jounalEntryDto.getDescription2());
			journalentry.setDescription3(jounalEntryDto.getDescription3());
			journalentry.setEnteredUser(LoginAction.getCurrentLogingUser().getId());
			journalentry.setStatus("INITIAL");
			journalentry.setFundId(jounalEntryDto.getFundId());
			try {
				journalentry.setJournalDate(format.parse(jounalEntryDto.getJournalDate()));
			} catch (ParseException e) {
				e.printStackTrace();
			}

			Object object = daoImpl.saveJournalEntry(journalentry);
			if (object instanceof Integer) {
				jounalEntryDto.setSuccess(true);
				jounalEntryDto.setResultMsg("SUCCESS");
			} else {
				jounalEntryDto.setSuccess(false);
				jounalEntryDto.setResultMsg("There is some error returned from the server.");
			}
		}

	}

	public List<JounalEntryDto> getAllJournals() {
		List<Journalentry> journalentries = daoImpl.getAllJournals();
		List<JounalEntryDto> entryDtos = new ArrayList<JounalEntryDto>();
		if (journalentries.size() > 0) {
			for (Journalentry journalentry : journalentries) {
				JounalEntryDto entryDto = new JounalEntryDto();
				entryDto.setId(journalentry.getId());
				entryDto.setAccountNo1(journalentry.getAccountNo1());
				entryDto.setAccountNo2(journalentry.getAccountNo2());
				entryDto.setAccountNo3(journalentry.getAccountNo3());
				entryDto.setAmount(journalentry.getAmount().toString());
				entryDto.setAmount1(journalentry.getAmount1().toString());
				entryDto.setAmount2(journalentry.getAmount2().toString());
				entryDto.setAmount3(journalentry.getAmount3().toString());
				entryDto.setCreditDebit(journalentry.getCreditDebit());
				entryDto.setCustoemrName(journalentry.getCustoemrName());
				if (journalentry.getCustomerType().equals("CUS"))
					entryDto.setCustomer(customerDaoImpl.getCustomerById(journalentry.getCustomerId()).getCode());
				else if (journalentry.getCustomerType().equals("ISU"))
					entryDto.setCustomer(issuerDaoImpl.getById(journalentry.getCustomerId()).getCode());
				entryDto.setCustomerId(journalentry.getCustomerId());
				entryDto.setCustomerType(journalentry.getCustomerType());
				entryDto.setDescription1(journalentry.getDescription1());
				entryDto.setDescription2(journalentry.getDescription2());
				entryDto.setDescription3(journalentry.getDescription3());
				entryDto.setJournalDate(journalentry.getJournalDate().toString());
				entryDto.setFundId(journalentry.getFundId());
				entryDto.setStatus(journalentry.getStatus());

				entryDtos.add(entryDto);
			}
		}
		return entryDtos;
	}

	public void confirmJournal(int journalId, JounalEntryDto jounalEntryDto) {

		Journalentry journalentry = daoImpl.getById(journalId);
		if (journalentry != null) {

			journalentry.setStatus("CONFIRM");
			journalentry.setConfirmedUser(LoginAction.getCurrentLogingUser().getId());
			List<MoneyTransaction> moneyTransactions = transactionModel.getJournalTransaction(jounalEntryDto, journalentry);
			Object object = daoImpl.updateJournal(journalentry, moneyTransactions);
			if (object instanceof Journalentry) {
				jounalEntryDto.setSuccess(true);
				jounalEntryDto.setResultMsg("SUCCESS");
			} else {
				jounalEntryDto.setSuccess(false);
				jounalEntryDto.setResultMsg("There is some error returned from the server.");
			}
		}

	}

	public void rejectJournal(int journalId, JounalEntryDto jounalEntryDto) {
		Journalentry journalentry = daoImpl.getById(journalId);
		if (journalentry != null) {
			journalentry.setStatus("REJECT");
			journalentry.setConfirmedUser(LoginAction.getCurrentLogingUser().getId());
			Object object = daoImpl.updateJournal(journalentry, null);
			if (object instanceof Journalentry) {
				jounalEntryDto.setSuccess(true);
				jounalEntryDto.setResultMsg("SUCCESS");
			} else {
				jounalEntryDto.setSuccess(false);
				jounalEntryDto.setResultMsg("There is some error returned from the server.");
			}

		}

	}

	public void updateJournalEntry(JounalEntryDto jounalEntryDto, int journalid) {
		Journalentry journalentry = daoImpl.getById(journalid);

		if (jounalEntryDto != null && journalentry != null) {

			journalentry.setAccountNo1(jounalEntryDto.getAccountNo1());
			journalentry.setAccountNo2(jounalEntryDto.getAccountNo2());
			journalentry.setAccountNo3(jounalEntryDto.getAccountNo3());
			if (jounalEntryDto.getAmount() != null) {
				String amount = jounalEntryDto.getAmount().replaceAll(",", "");
				double d = Double.parseDouble(amount);
				journalentry.setAmount(BigDecimal.valueOf(d));
			}
			if (jounalEntryDto.getAmount1() != null) {
				String amount = jounalEntryDto.getAmount1().replaceAll(",", "");
				double d = Double.parseDouble(amount);
				journalentry.setAmount1(BigDecimal.valueOf(d));
			}
			if (jounalEntryDto.getAmount2() != null) {
				String amount = jounalEntryDto.getAmount2().replaceAll(",", "");
				double d = Double.parseDouble(amount);
				journalentry.setAmount2(BigDecimal.valueOf(d));
			}
			if (jounalEntryDto.getAmount3() != null) {
				String amount = jounalEntryDto.getAmount3().replaceAll(",", "");
				double d = Double.parseDouble(amount);
				journalentry.setAmount3(BigDecimal.valueOf(d));
			}
			journalentry.setCreditDebit(jounalEntryDto.getCreditDebit());
			journalentry.setCustoemrName(jounalEntryDto.getCustoemrName());
			journalentry.setCustomerId(jounalEntryDto.getCustomerId());
			journalentry.setCustomerType(jounalEntryDto.getCustomerType());
			journalentry.setDescription1(jounalEntryDto.getDescription1());
			journalentry.setDescription2(jounalEntryDto.getDescription2());
			journalentry.setDescription3(jounalEntryDto.getDescription3());
			journalentry.setEnteredUser(LoginAction.getCurrentLogingUser().getId());
			journalentry.setFundId(jounalEntryDto.getFundId());
			try {
				journalentry.setJournalDate(format.parse(jounalEntryDto.getJournalDate()));
			} catch (ParseException e) {
				e.printStackTrace();
			}

			Object object = daoImpl.updateJournal(journalentry, null);
			if (object instanceof Journalentry) {
				jounalEntryDto.setSuccess(true);
				jounalEntryDto.setResultMsg("SUCCESS");
			} else {
				jounalEntryDto.setSuccess(false);
				jounalEntryDto.setResultMsg("There is some error returned from the server.");
			}
		}

	}
}
